// author: killer
// blog: blog.csdn.net/sabcdefg
// netpt.net
// 武林秘籍 ^^^ 目录 ^^^
#include "mymultilanguage.h"
#include <QApplication>
#include <QFile>

MyMultiLanguage::MyMultiLanguage()
{
	m_translator = NULL;
}

MyMultiLanguage::~MyMultiLanguage()
{

}



void MyMultiLanguage::Init(QString strLanguage)
{
	Q_ASSERT(NULL == m_translator);
    Switch(strLanguage);
}

void MyMultiLanguage::Switch(QString strNewLang)
{
    if (0 != m_strCurLanguage.compare(strNewLang))
    {
        QString strQM = QString(":/res/language/%1.qm").arg(strNewLang);

        if (QFile(strQM).exists())
        {
            QTranslator* translator = new QTranslator;
            if (translator->load(strQM))
            {
                m_strCurLanguage = strNewLang;

                if (NULL != m_translator)
                {
                    qApp->removeTranslator(m_translator);
                    delete m_translator;
                }

                m_translator = translator;
                qApp->installTranslator(m_translator);
            }
            else
            {
                delete translator;
                translator = NULL;
            }
        }

    }
}

QString MyMultiLanguage::GetCurLanguage()
{
    return m_strCurLanguage;
}


void MyMultiLanguage::UnInit()
{
	if (NULL != m_translator)
	{
		qApp->removeTranslator(m_translator);
		delete m_translator;
		m_translator = NULL;
	}
}
